<template>
	<div class="devtool-metadata-container">
		<el-dialog title="修改元数据" v-model="isShowDialog" >
			<el-form ref="editMetadataFormRef" :model="formData" :rules="formRules" size="small" label-width="100px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="元数据名称" prop="columnName">
							<el-input v-model="formData.columnName" placeholder="请输入元数据名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="元数据描述" prop="columnComment">
							<el-input v-model="formData.columnComment" placeholder="请输入元数据描述" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="元数据类型" prop="columnType">
							<el-select v-model="formData.columnType" placeholder="请选择元数据类型" clearable class="w100">
								<el-option label="字符串(String) 例abcABC123" value="String"></el-option>
								<el-option label="整数(Integer) 例-2^31 ~ 2^31 - 1" value="Integer"></el-option>
								<el-option label="长整数(Long) 例-2^63 ~ -2^63 - 1" value="Long"></el-option>
								<el-option label="浮点数(Double) 例2^-1024 ~ 2^1024 - 1" value="Double"></el-option>
								<el-option label="时间(Date) 例：2020-05-15 12:50:20" value="Date"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="small">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="small">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { editMetadata } from '/@/api/devtool/metadata/index';
export default {
	name: 'devtool/metadata/edit',
	setup(props, context) {
		const editMetadataFormRef = ref();
		const state = reactive({
			isShowDialog: false,
			formData: {},
			formRules : {
				columnName: { required: true, message: '元数据名称不能为空', trigger: 'blur' },
				columnComment: { required: true, message: '元数据描述不能为空', trigger: 'blur' },
				columnType: { required: true, message: '元数据类型不能为空', trigger: 'change' },
			}
		});
		// 打开弹窗
		const openDialog = (row: any) => {
			state.isShowDialog = true;
			if(row){
				state.formData = Object.assign({}, row);
			}
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			editMetadataFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 保存
		const onSubmit = () => {
			editMetadataFormRef.value.validate(valid => {
				if(valid){
					let params = {
						id: state.formData.id,
						columnName: state.formData.columnName,
						columnComment: state.formData.columnComment,
						columnType: state.formData.columnType,
						remark: state.formData.remark
					}
					editMetadata(params).then((res) => {
						if(res.code === 0){
							ElMessage.success('保存成功');
							context.emit('submit');
							closeDialog(); // 关闭弹窗
						}
					});
				}
			});
		};
		
		return {
			editMetadataFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>
